<?php
/**
 *******************************************************************
 ************* ─────────────────── *********************************
 ************* ── 代发货平台系统官方正式版 ── **********************
 ************* ─────────────────── *********************************
 ************* 官方网站：http://order.ai-pay.com/   ****************
 ************* 官方店铺：http://web101.taobao.com/  ****************
 ************* 程序开发：淘宝小赢家                 ****************
 ************* 客户服务：[淘宝旺旺] 淘宝小赢家 *********************
 ************* 技术支持：[QQ] 1752174348            ****************
 *******************************************************************
 *************   官方正版 *** 版权所有 *** 盗版必究   **************
 *******************************************************************
 */
class produce extends spController{
	public function __construct() {
		//登录验证	
		if(!isset($_SESSION['IS_LOGIN']) || !$_SESSION['IS_LOGIN']){
			$this->jump (spUrl ('login','index'));
		}	
		parent::__construct();
		$this -> _controller = 'produce';
		$this -> _pk = spClass('MProduce') -> pk;
		$this -> _attributes = spClass('MProduce') -> getAttributes();
		$this -> _attributeFormset = spClass('MProduce') -> getAttributeFormsets();
		spAddViewFunction('cbForm',array('LibCodeBuilder','cbFormWidget'));
		$this -> savesession = $_SESSION['IS_LOGIN'];
		$this -> savesessionid = $_SESSION['LOGIN_ID'];
		$this->spAcl = spClass("spAcl")->get();
	}
	
	public function index(){
		$this->_attributes = spClass('MProduce') -> getUnFilteAttributes();
		$this -> tpl_page = $page = $this -> spArgs('page',1);
		$this -> tpl_datas = spClass('MProduce') -> spPager($page, 10) -> findAll();
		$this -> tpl_pager = spClass('MProduce') -> spPager() -> getPager();
		$this -> display('produce/search.html');
	}
	
	public function view(){
		$condition = array();
		$condition[$this->_pk] = $this -> spArgs('id');
		$this -> tpl_data = spClass('MProduce') -> find($condition);
		$this -> display('produce/view.html');
	}
	
	public function create(){
		if($_POST){
			$rows = spClass('spArgs')->get($this->_controller, null, 'post');

			if($_FILES["produce"]["name"]['bigImg']){
				//大图
				if ((($_FILES["produce"]["type"]['bigImg'] == "image/gif")
				|| ($_FILES["produce"]["type"]['bigImg'] == "image/jpeg")
				|| ($_FILES["produce"]["type"]['bigImg'] == "image/png"))
				&& ($_FILES["produce"]["size"]['bigImg'] < 30000))
				{
				  if (!$_FILES["produce"]["error"]['bigImg'])
					{
						if (!file_exists("img/" . $_FILES["produce"]["name"]['bigImg'])){
							$rows['bigImg'] = $_FILES["produce"]["name"]['bigImg'];
							move_uploaded_file($_FILES["produce"]["tmp_name"]['bigImg'],"img/good_pic/" . $_FILES["produce"]["name"]['bigImg']);
						}
					}
				}else{
					$this ->error ('图片格式不正确或者图片超过30K!', 'javascript:history.go(-1)');
				}
			}
			if(($id = spClass('MProduce')->create($rows)))
//				$this ->success ('新增记录成功',  spUrl ($this->_controller, 'view', array('id'=>$id)));
				$this ->success ('新增记录成功',  spUrl ($this->_controller, 'search'));
			else
				$this ->error ('新增记录失败', 'javascript:history.go(-1)');
		}
		$this ->display('produce/create.html');
	}
	
	public function update(){
		$condition = array();
		$condition[$this->_pk] = $this -> spArgs('id');
		if($_POST){
			$rows = spClass('spArgs')->get($this->_controller, null, 'post');
			
			if($_FILES["produce"]["name"]['bigImg']){
				//大图
				if ((($_FILES["produce"]["type"]['bigImg'] == "image/gif")
				|| ($_FILES["produce"]["type"]['bigImg'] == "image/jpeg")
				|| ($_FILES["produce"]["type"]['bigImg'] == "image/png"))
				&& ($_FILES["produce"]["size"]['bigImg'] < 30000))
				{
				  if (!$_FILES["produce"]["error"]['bigImg'])
					{
						if (!file_exists(APP_PATH."/img/" . $_FILES["produce"]["name"]['bigImg'])){
							$rows['bigImg'] = $_FILES["produce"]["name"]['bigImg'];
							move_uploaded_file($_FILES["produce"]["tmp_name"]['bigImg'],APP_PATH . "/img/good_pic/" . $_FILES["produce"]["name"]['bigImg']);
						}
					}
				}else{
					$this ->error ('图片格式不正确或者图片超过30K!', 'javascript:history.go(-1)');
				}
			}
			
			if(spClass('MProduce')->update($condition,$rows))
//				$this ->success ('编辑记录成功',  spUrl ($this->_controller, 'view', array('id'=>$this -> spArgs('id'))));
                $this ->success ('编辑记录成功',  spUrl ($this->_controller, 'search'));
			else
				$this ->error ('编辑记录失败', 'javascript:history.go(-1)');
		}
		$this -> tpl_data = spClass('MProduce') -> find($condition);
		$this ->display('produce/update.html');
	}
	
	public function delete(){
		$condition = array();
		$condition[$this->_pk] = $this -> spArgs('id');
		$success = spClass('MProduce') -> delete($condition);
		if($success)
			$this ->success('删除记录成功', $this->spArgs('redirect','javascript:history.go(-1)'));
		else
			$this ->error('删除记录失败', $this->spArgs('redirect','javascript:history.go(-1)'));
	}
	
	public function search(){
		$this->_attributes = spClass('MProduce') -> getUnFilteAttributes();
		if($_POST){
			$this -> tpl_keyword = $conditions = spClass('spArgs')->get($this->_controller, null, 'post');
			$this -> tpl_searchType = $searchType = spClass('spArgs')->get($this->_controller.'_searchType', null, 'post');
			$where = array();
			foreach($conditions as $key => $value){
				if(empty($value)) continue;
				if(empty($this->_attributes[$key])) continue;
				if($searchType[$key] == 1) $where[] = ' '.$key.' LIKE '.spClass('MProduce')->escape($value).' ';
				else $where[] = ' '.$key.' LIKE '.spClass('MProduce')->escape('%'.$value.'%').' ';
			}
			$this -> tpl_page = $page = $this -> spArgs('page',1);
			$this -> tpl_datas = spClass('MProduce') -> spPager($page, 10) -> findAll(implode('AND',$where));
			$this -> tpl_pager = spClass('MProduce') -> spPager() -> getPager();
		}else{
			$this -> tpl_page = $page = $this -> spArgs('page',1);
			$this -> tpl_datas = spClass('MProduce') -> spPager($page, 10) -> findAll();
			$this -> tpl_pager = spClass('MProduce') -> spPager() -> getPager();
		}
		$this -> display('produce/search.html');
	}
}